from django.shortcuts import render
from course import models,courseser
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from django.db.models import Q
# Create your views here.

#默认首页数据展示
class HomeCate(APIView):
    def get(self,request):
        catelist= models.CategoryModel.objects.filter(recommend=True,parent=0).order_by('order')
        ser=courseser.CategorySerializer(catelist,many=True)
        data=ser.data
        for item in data:
            item["son"]=item["son"][0:3]
        return Response({"mes":"ok","data":ser.data})
#获取当前分类下子分类以及推荐现在在首页的课程信息
class CateCourse(APIView):
    def get(self,request):
        parentid = request.query_params.get("pid")
        catelist = models.CategoryModel.objects.filter(id=parentid).first()
        ser=courseser.CategorySerializer(catelist)
        # ser.data["son"]#[1,2,3,4,5]
        # models.课程表模型.objects.filter(外键__in=[1,2,3,4,5], recommend=True).order_by('order')[:4]
        #先获取当前一级分类的二级分类id [son,son,son,son]
        sonlist=[]
        for item in ser.data["son"]:
            sonlist.append(item["id"])
        #print(sonlist)
        #利用filter去course表中检索分类id等于[son,son,son,son]的数据
        homecourse=models.CourseModel.objects.filter(category__in=sonlist,is_home=True).order_by('order')[:4]
        coursedata=courseser.CourseSerializer(homecourse,many=True).data
        datalist=ser.data
        datalist["course"]=coursedata
        return Response({"mes":"ok","data":datalist})


#获取到所有的一级分类
class GetParentCate(APIView):
    def get(self,request):
        parentcate=models.CategoryModel.objects.filter(parent=0)
        ser=courseser.CategorySerializer(parentcate,many=True)
        return Response({"mes":"ok","data":ser.data})
class GetSonCate(APIView):
    def get(self,request):
        #默认的展示所有的二级分类  如果给参数 就是展示当前参数下的二级分类
        parentid=request.query_params.get("pid")
        if parentid:
            #根据条件拿到数据
            sondata=models.CategoryModel.objects.filter(parent=parentid)
        else:
            #展示全部数据
            sondata=models.CategoryModel.objects.filter(parent__gt=0)
        ser=courseser.CateSerializer(sondata,many=True)
        return Response({"mes":"ok","data":ser.data})
#课程的分页类
class CoursePage(PageNumberPagination):
    page_size=2
    page_size_query_param="size"
    page_query_param="page"

#获取所有的课程信息
class GetCourse(ListAPIView):
    serializer_class=courseser.CourseSerializer
    pagination_class=CoursePage
    #根据条件进行筛选
    # def get_queryset(self):
    #     queryset = models.CourseModel.objects.filter(isshow=True).order_by("order")
    #     #获取到前端传过来的参数
    #     cateid=self.request.query_params.get("cateid")
    #     directionid = self.request.query_params.get("directionid")
    #     if cateid:
    #         #如果cateid不为空 就不用管 directionid 只是检索 cateid
    #         queryset = models.CourseModel.objects.filter(isshow=True,category=cateid).order_by("order")
    #     else:
    #         #如果cateid为空 directionid不为空
    #         if directionid:
    #             #根据directionid拿到所有的二级分类  [son1,son2,son3]
    #             son=models.CategoryModel.objects.filter(parent=directionid)
    #             sondata=[]
    #             for i in son:
    #                 sondata.append(i.id)
    #             queryset = models.CourseModel.objects.filter(isshow=True, category__in=sondata).order_by("order")
    #     return queryset
    def get_queryset(self):
        query=Q(isshow=True)
        #获取到前端传过来的参数
        cateid=self.request.query_params.get("cateid")
        directionid = self.request.query_params.get("directionid")
        level=self.request.query_params.get("level")
        #前端传递的参数
        orderdata=self.request.query_params.get("ordering")

        if level:
            query=query&Q(level=level)
        if cateid:
            #如果cateid不为空 就不用管 directionid 只是检索 cateid
            query=query&Q(category=cateid)
        else:
            #如果cateid为空 directionid不为空
            if directionid:
                #根据directionid拿到所有的二级分类  [son1,son2,son3]
                son=models.CategoryModel.objects.filter(parent=directionid)
                sondata=[]
                for i in son:
                    sondata.append(i.id)
                query=query&Q(category__in=sondata)
        if orderdata:
            queryset=models.CourseModel.objects.filter(query).order_by("order",orderdata)
        else:
            queryset=models.CourseModel.objects.filter(query).order_by("order")
        return queryset
class GetDetail(APIView):
    def get(self,request):
        id=request.query_params.get("id")
        data=models.CourseModel.objects.filter(id=id).first()
        ser=courseser.CourseSerializer(data)
        return Response({"msg":"success","data":ser.data})


